from django.db import models
from django.conf import settings
from coach.models.ai_patient_models import AIPatient

class ChatRecord(models.Model):
    """实习医生与AI病人的聊天记录模型"""
    student = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='chat_records',
        verbose_name="实习医生"
    )
    ai_patient = models.ForeignKey(
        AIPatient,
        on_delete=models.CASCADE,
        related_name='chat_records',
        verbose_name="AI病人"
    )
    # 消息发送者类型：student-实习医生，ai-人工智能病人
    sender_type = models.CharField(
        max_length=10,
        choices=[('student', '实习医生'), ('ai', 'AI病人')],
        verbose_name="发送者类型"
    )
    message_content = models.TextField(verbose_name="消息内容")
    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name="发送时间"
    )

    class Meta:
        verbose_name = "聊天记录"
        verbose_name_plural = "聊天记录"
        ordering = ['created_at']  # 按时间顺序排列
        db_table = "chat_record"

    def __str__(self):
        return f"{self.get_sender_type_display()} - {self.created_at.strftime('%Y-%m-%d %H:%M')}"